//
//  LTCTestView.m
//  UIKitCatalog
//
//  Created by LiJonius on 16/1/28.
//  Copyright © distance16年 f. All rights reserved.
//

#import "LTCTestView.h"
#import <QuartzCore/QuartzCore.h>
#import <CoreText/CoreText.h>
//表格 距离 视图 上下左右的 距离
#define distance 20
@implementation LTCTestView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor=[UIColor cyanColor];
    }
    return self;
}
- (void)drawRect:(CGRect)rect {
    //获取上下文
    CGContextRef context=UIGraphicsGetCurrentContext();
    //横线数量
    int lineCount = 6;
    //两条线之间的距离
    float lineDistance = (self.frame.size.height-40)*1.0 / (lineCount-1);
    for (int i = 0 ; i<lineCount ; i++) {
        //设置线的宽度
        CGContextSetLineWidth(context, 1.0);
        //设置线条颜色
        CGContextSetRGBStrokeColor(context, 0.1, 0.3, 0.5, 1.0);
        //设置线的起点
        CGContextMoveToPoint(context, distance, i*lineDistance+distance);
        //设置线条的路径
        CGContextAddLineToPoint(context, self.frame.size.width-distance, i*lineDistance+distance);
        CGContextStrokePath(context);
    }
    
    //竖线  数量 与横线 一致
    
    for (int i = 0;  i< lineCount; i++) {
        
        CGContextMoveToPoint(context,distance+i*lineDistance, distance);
        CGContextAddLineToPoint(context,distance+i*lineDistance, self.frame.size.width-distance);
        CGContextStrokePath(context);
        
    }
    
    //x轴坐标
    for (int i = 0 ; i < lineCount; i++ ) {
        NSString* str=[NSString stringWithFormat:@"%d",i];
        //生成一个dic  ，里面放入多个键值对 ： obj1,@"key1", obj2,@"key2"  ...
        NSDictionary* dic=[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Helvetica-Bold" size:10],NSFontAttributeName, nil];
        //创建一个富文本 attrString
        NSAttributedString * attrString=[[NSAttributedString alloc] initWithString:str attributes:dic];
        
        CTLineRef lineref=CTLineCreateWithAttributedString((__bridge CFAttributedStringRef) attrString);
        //下面打代码 控制 字体，如果没有此行代码，字体是翻转的
        CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0, -1.0));
        //绘制 字符串的 坐标
        CGContextSetTextPosition(context, i*lineDistance+distance-5, self.frame.size.height-10);
        
        CTLineDraw(lineref, context);
        
        CFRelease(lineref);
        
    }
    
    //y 轴坐标
    for (int i = 1; i<lineCount; i++ ) {
        NSString* str=[NSString stringWithFormat:@"%d",i];
        //生成一个dic  ，里面放入多个键值对 ： obj1,@"key1", obj2,@"key2"  ...
        NSDictionary* dic=[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Helvetica-Bold" size:10],NSFontAttributeName, nil];
        //创建一个富文本 attrString
        NSAttributedString * attrString=[[NSAttributedString alloc] initWithString:str attributes:dic];
        
        CTLineRef lineref=CTLineCreateWithAttributedString((__bridge CFAttributedStringRef) attrString);
        //下面打代码 控制 字体，如果没有此行代码，字体是翻转的
        CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1.0, -1.0));
        //绘制 字符串的 坐标
        CGContextSetTextPosition(context, 10, (lineCount-i-1)*lineDistance+distance+5);
        
        CTLineDraw(lineref, context);
        
        CFRelease(lineref);
    }
    NSArray* points=@[@"4",@"3",@"1",@"2",@"0"];
    for (int i = 0 ; i<points.count; i++) {
        //画点
        //float j= arc4random() % (i+1);
        int j =[points[i] intValue];
        //设置点的 颜色
        CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
        //设置点的坐标 和大小
        CGContextFillEllipseInRect(context, CGRectMake(lineDistance*i+distance, (self.frame.size.height-40)-lineDistance * j, 5, 5));
        CGContextStrokePath(context);
        
        
        //划线 ：把点  用线连起来
        if (i<points.count && i>0) {
            
            CGContextSetLineWidth(context, 1.0);
            CGContextMoveToPoint(context, lineDistance*(i-1)+distance +2, (self.frame.size.height-40)-lineDistance * (float)([points[i-1] intValue]) + 2);
            //设置线条颜色
            //CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
            [[UIColor redColor] set];
            CGContextAddLineToPoint(context, lineDistance*(i)+distance +2, (self.frame.size.height-40)-lineDistance * (float)([points[i] intValue]) + 2);
            CGContextStrokePath(context);
        }

        
    }
    
}

@end
